<?php
class ArticleController extends AdminAppController{
    // public $uses = array('Admin.Category');
    
    function index($parent_id = null)
    {
        $this->loadModel('Admin.Article');
        $article = $this->Article->get_article('all', $parent_id);
        
        $list_article = array();
        foreach ($article as $art) {
            $temp = array();
            $temp['article_id'] = $art['article']['article_id'];
            $temp['title'] = $art['article_desc']['title'];
            $temp['sort_order'] = $art['article']['sort_order'];
            $temp['status'] = $art['article']['status'];
            $temp['image'] = $art['image']['name'];
            $list_article[] = $temp;
        }
    
        $parent_category = $this->Article->get_parent_category();
        
        $this->set('parent_id', $parent_id);
        $this->set('list_article', $list_article);
        $this->set('parent_category', $parent_category);
    }
    
    function add($parent_id = null)
    {
        $this->loadModel('Admin.Language');
        $this->loadModel('Admin.Category');
        
        $list_language = $this->Language->get_language();
        $this->set('list_language', $list_language);

        
        $category = $this->Category->get_all_category('all');
        $list_category = $this->Category->filter_category($category);
        $this->set('list_category', $list_category);
        
        $this->set('parent_id', $parent_id);
        if($this->request->isPost())
        {
            $this->loadModel('Admin.Article');
            $this->loadModel('Admin.ArticleDesc');
            $this->loadModel('Admin.Image');
            $data = array(
                        'category_id' => $_POST['category_id'],
                        'date_added' => date("Y-m-d H:i:s"),
                        'date_modified' => date("Y-m-d H:i:s"),
                        'status' => $_POST['status'],
                        'other' => $this->request->data['Article']['other'],
                        'sort_order' => $this->request->data['Article']['sort_order'],
                        );
            $article_id = $this->Article->add($data);

            foreach($list_language as $language)
            {
                $language = $language['language'];
                $lang_code = $language['lang_code'];
                $data = array(
                        'article_id' => $article_id,
                        'title' => $_POST["title_$lang_code"],
                        'description' => $_POST["description_$lang_code"],
                        'long_description' => $_POST["long_description_$lang_code"],
                        'meta_title' => $_POST["meta_title_$lang_code"],
                        'meta_description' => $_POST["meta_description_$lang_code"],
                        'meta_keywords' => $_POST["meta_keywords_$lang_code"],
                        'lang_code' => $lang_code,
                        );
                $this->ArticleDesc->add($data);
            }
            
            $main_image = $_POST['main_image'];
            $other_image = isset($_POST['other_image']) ? $_POST['other_image'] : array();
            $order_image = isset($_POST['order_image']) ? $_POST['order_image'] : array();
            $data['main_image'] = $main_image;
            $data['other_image'] = $other_image;
            $data['order_image'] = $order_image;
            $this->Image->add($article_id, 'article', $data);
            
            $this->loadModel('Admin.Link');
            foreach($list_language as $language)
            {
                $language = $language['language'];
                $lang_code = $language['lang_code'];
                
                $data = array(
                        'link' => $_POST["link_$lang_code"],
                        'controller' => 'news',
                        'method' => 'detail',
                        'post_id' => $article_id,
                        'type' => 'article',
                        'lang_code' => $lang_code
                );
                $this->Link->add($data);
            }
            
            
            
            $this->Session->setFlash(
                                'Thêm mới bài viết thành công',
                                'default',
                                array('class' => 'alert alert-success')
                            );
            $this->redirect(array("controller" => "article", 
                                  "action" => "index", $parent_id));
        }
    }
    
    function edit($article_id = "", $parent_id = null)
    {
        if($article_id == ""){
            $this->redirect(array("controller" => "article", 
                                  "action" => "index")
                            );
            exit;
        }
        $this->set('article_id', $article_id);
        $this->loadModel('Admin.Language');
        $this->loadModel('Admin.Category');
        $this->loadModel('Admin.Image');
        $this->loadModel('Admin.Article');
        $this->loadModel('Admin.ArticleDesc');

        $article_info = $this->Article->get_article_by_id($article_id, 'all');
        $this->set('article_info', $article_info);
        
        $list_language = $this->Language->get_language();
        $this->set('list_language', $list_language);

        $category = $this->Category->get_all_category('all');
        $list_category = $this->Category->filter_category($category);
        $this->set('list_category', $list_category);
        
        $list_image = $this->Image->get($article_id, 'article');
        $this->set('list_image', $list_image);
        
        $this->set('parent_id', $parent_id);
        if($this->request->isPost())
        {
            $data = array(
                        'category_id' => $_POST['category_id'],
                        'date_modified' => "'".date("Y-m-d H:i:s")."'",
                        'status' => $_POST['status'],
                        'other' => "'".$this->request->data['Article']['other']."'",
                        'sort_order' => "'".$this->request->data['Article']['sort_order']."'",
                        );
            $this->Article->edit($article_id, $data);
            
            foreach($list_language as $language)
            {
                $language = $language['language'];
                $lang_code = $language['lang_code'];
                $data = array(
                        'title' => "'".$_POST["title_$lang_code"]."'",
                        'description' => "'".$_POST["description_$lang_code"]."'",
                        'long_description' => "'".$_POST["long_description_$lang_code"]."'",
                        'meta_title' => "'".$_POST["meta_title_$lang_code"]."'",
                        'meta_description' => "'".$_POST["meta_description_$lang_code"]."'",
                        'meta_keywords' => "'".$_POST["meta_keywords_$lang_code"]."'",
                        );
                $this->ArticleDesc->edit($article_id, $lang_code, $data);
            }
            
            
            $main_image = $_POST['main_image'];
            $other_image = isset($_POST['other_image']) ? $_POST['other_image'] : array();
            $order_image = isset($_POST['order_image']) ? $_POST['order_image'] : array();
            $data['main_image'] = $main_image;
            $data['other_image'] = $other_image;
            $data['order_image'] = $order_image;
            $this->Image->edit($article_id, 'article', $data);
            
            
            $this->loadModel('Admin.Link');
            $this->Link->deleteAll(array('post_id' => $article_id, 'type' => 'article'));
            foreach($list_language as $language)
            {
                $language = $language['language'];
                $lang_code = $language['lang_code'];
                
                $data = array(
                        'link' => $this->request->data["link_$lang_code"],
                        'controller' => 'article',
                        'method' => 'index',
                        'post_id' => $article_id,
                        'type' => 'article',
                        'lang_code' => $lang_code
                );
                $this->Link->add($data);
            }
            
            $this->Session->setFlash(
                                'Sửa bài viết thành công',
                                'default',
                                array('class' => 'alert alert-success')
                            );
            
            $this->redirect(array("controller" => "article", 
                                  "action" => "index", $parent_id));
            
        }
    }

    
}